﻿package com.cenfun.ui{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	public class SB extends UI {
		//滚动条事件
		public static const CHANGE:String = "change";
		
		//常量设置
		public var arrowHeight:Number = 14;
		public var thumbHeight:Number = 30;
		public var scrollSize:Number = 20;
		public var scrollDelay:Number = 100;
		//
		private var timeid:uint;
		//轨迹高度
		private var trackHeight:Number;
		//内容的位置和总大小
		private var position:Number;
		private var duration:Number;
		
		private var _percent:Number;
		
		//内部组件
		private var btup:BT;
		private var btdown:BT;
		private var thumb:BT;
		private var track:BT;
		
		private var draging:Boolean;
		private var dragpos:Number;
		public function SB():void {
			tw = 15;
			th = 100;
			//组件
			track = new BT(ScrollTrack_skin, ScrollTrack_skin, ScrollTrack_skin);
			btup = new BT(ScrollArrowUp_upSkin, ScrollArrowUp_overSkin, ScrollArrowUp_downSkin);
			btup.autoRepeat = true;
			btdown = new BT(ScrollArrowDown_upSkin, ScrollArrowDown_overSkin, ScrollArrowDown_downSkin);
			btdown.autoRepeat = true;
			thumb = new BT(ScrollThumb_upSkin, ScrollThumb_overSkin, ScrollThumb_downSkin);
			addChild(track);
			addChild(btup);
			addChild(btdown);
			addChild(thumb);
			//初始化更新到默认位置
			update(0, arrowHeight * 2 + thumbHeight);
			//事件
			btup.addEventListener(BT.BUTTON_DOWN, arrowDown, false, 0, true);
			btdown.addEventListener(BT.BUTTON_DOWN, arrowDown, false, 0, true);
			track.addEventListener(MouseEvent.CLICK, trackClick, false, 0, true);
			thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown, false, 0, true);
		}

		private function arrowDown(e:UE):void {
			e.stopImmediatePropagation();
			var pos:Number = position + scrollSize;
			if (e.currentTarget == btup) {
				pos = position - scrollSize;
			}
			
			
			
			
			
			
			
			
			if (position != pos) {
				position = pos;
				percent = position / duration;
				sendEvent();
			}
		}
		
		private function trackClick(e:MouseEvent):void {
			setPosition(track.mouseY);
		}
		
		public function set scrollPosition(v:Number):void {
			var pos:Number = v;
			setPosition(pos);
		}
		public function get scrollPosition():Number {
			return Math.round(trackHeight * percent);
		}
		
		
		public function setPosition(pos:Number):void {
			pos = Math.min(trackHeight, Math.max(0, pos));
			percent = pos / trackHeight;
			position = Math.round(duration * percent);
			sendEvent();
		}
		
		
		private function thumbDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
			dragpos = mouseY;
		}
		private function thumbMove(e:MouseEvent):void {
			if (dragpos == mouseY) {
				return;
			}
			if (draging) {
				//发送位置改变事件
				thumbChange();
			} else {
				draging = true;
				var rect:Rectangle = new Rectangle(0, arrowHeight, 0, trackHeight - thumbHeight);
				thumb.startDrag(false, rect);
			}
		}
		private function thumbUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, thumbUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
			if (draging) {
				thumb.stopDrag();
				draging = false;
				thumbChange();
			}
		}
		
		private function thumbChange():void {
			_percent = (thumb.y - arrowHeight) / (trackHeight - thumbHeight);
			position = Math.round(duration * percent);
			sendEvent();
		}
		
		private function sendEvent():void {
			var ue:UE = new UE(SB.CHANGE, percent || 0);
			dispatchEvent(ue);
		}
		
		public function update(pos:Number, dur:Number):void {
			//留着有用，高度更新时更新滑块的高度
			if (position != pos || duration != dur) {
				position = pos;
				duration = dur;
				//位置百分比
				_percent = pos / dur;
				layout();
			}
		}
		
		public function set percent(v:Number):void {
			if (_percent != v) {
				_percent = v;
				layout();
			}
		}
		public function get percent():Number {
			return _percent;
		}
		
		
		override public function layout():void {
			//更新轨迹高度
			trackHeight = th - arrowHeight * 2;
			//计算滑块高度
			var per:Number = th / duration;
			
			//trace(th, duration);
			
			thumbHeight = Math.round(trackHeight * per);
			thumbHeight = Math.min(trackHeight, Math.max(arrowHeight, thumbHeight));
			
			//trace(per,trackHeight,thumbHeight);
			
			//计算滑块位置
			var pos:Number = Math.round((trackHeight - thumbHeight) * percent);
			pos = Math.min(trackHeight - thumbHeight, Math.max(0, pos));

			//更新布局和大小
			track.move(0, arrowHeight);
			track.size(tw, trackHeight);
			
			btup.move(0, 0);
			btup.size(tw, arrowHeight);
			
			btdown.move(0, th - arrowHeight);
			btdown.size(tw, arrowHeight);
			
			thumb.move(0, arrowHeight + pos);
			thumb.size(tw, thumbHeight);
			
		}
		
		
	}
}